package cn.itsource.blog.controller;
import cn.itsource.blog.domain.Comment;
import cn.itsource.blog.service.ICommentService;
import cn.itsource.until.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


/**
 * 评论接口
 * */
@RestController
@RequestMapping("/comment")
@Api(tags = "评论接口类") // 用于描述整个API接口的信息，包括API的标题、描述等
public class CommentController {
    @Autowired
    private ICommentService commentService;


    /**
     * 删除单个
     * */
    @ApiOperation(value = "删除单个")
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id")String id){
        try {
            commentService.delete(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 添加或修改
     * */
    @ApiOperation(value = "添加或修改")
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody Comment comment){
        try {
            if (comment.getId() == null) {
                commentService.add(comment);
            }else {
                commentService.update(comment);}
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 查询文章类型
     * */
    @ApiOperation(value = "添加或修改")
    @GetMapping("/{articleId}")
    public List<Comment> findByArticleId(@PathVariable("articleId") Long articleId){
      return commentService.findByArticleId(articleId);
    }




}
